package cn.liu.hrm.service.impl;

import cn.liu.hrm.domain.Employee;
import cn.liu.hrm.domain.Tenant;
import cn.liu.hrm.dto.TenantDto;
import cn.liu.hrm.mapper.EmployeeMapper;
import cn.liu.hrm.mapper.TenantMapper;
import cn.liu.hrm.service.ITenantService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Mr.liu
 * @since 2020-03-26
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void entering(TenantDto tenantDto) {

        Tenant tenant = tenantDto.getTenant();
        Employee employee = tenantDto.getEmployee();
        Long mealId = tenantDto.getMealId();

        //判断参数信息，校验是否合法
        if (!StringUtils.hasLength(tenant.getCompanyName())){
            throw new RuntimeException("公司名字不能为空");
        }
        //1.先保存租户信息
        Date date = new Date();

        tenant.setRegisterTime(date);
        tenant.setState(false);
        baseMapper.insert(tenant);

        //机构id
        Long tenantId = tenant.getId();

        //2.保存管理员信息
        employee.setInputTime(date);
        employee.setState(Employee.STATE_NORMAL);
        employee.setTenantId(tenantId);
        employee.setType(Employee.TYPE_ADMIN);
        employeeMapper.insert(employee);

        //3.保存租户和套餐关系
        baseMapper.insertRelationWithMeal(mealId,tenantId,Tenant.STATE_PAY_MEAL_UNPAY);
    }
}
